#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;



 // Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    int minDiffInBST(TreeNode* root) {
        //二叉搜索树执行中序遍历
        int result = INT16_MAX;
        int pre = -1;
        func(root,pre,result);
        return result;

    }
    void func(TreeNode* root,int& pre,int& result){
        if(root==nullptr){
            return;
        }
        func(root->left,pre,result);
        if(pre!=-1){
            result = min(result,root->val-pre);
        }
        pre = root->val;
        func(root->right,pre,result);
    }
};

int main(){
    Solution S;
    TreeNode* root = new TreeNode(27);
    // root->left = new TreeNode(0);
    root->right = new TreeNode(34);
    // root->right->left = new TreeNode(12);
    root->right->right = new TreeNode(58);
    root->right->right->left = new TreeNode(50);
    root->right->right->left->left = new TreeNode(44);
    cout<<S.minDiffInBST(root)<<endl;
}